home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 15625 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.7 KB

  1. Path: news.minn.net!news
  2. From: gruch@minn.net
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Array indexing with malloc
  5. Date: Sat, 20 Apr 1996 13:02:03 GMT
  6. Organization: Minn Net
  7. Message-ID: <4lan7j$1op@cobra.Minn.Net>
  8. References: <96110.093936F0O@psuvm.psu.edu>
  9. NNTP-Posting-Host: dialup-198.minn.net
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12. Tim Benner <F0O@psuvm.psu.edu> wrote:
  13.  
  14. >   I write c programs to collect online data, and I always malloc a block
  15. >of memory to hold the data.  I set the memory block up such that each
  16. >row is a different time point, and each column is a different channel.
  17. >   The formula I use to do this is:
  18. >         RawData[CurrentPoint*NumChannels+Channel]
  19. >   This works fine, except I don't like having to do the multiplication
  20. >every time I want to access a point in the memory block.  I tried setting
  21. >up the memory block by mallocing a structure.  This also works, and I
  22. >believe is neater in implementation, but seems to have a limit of about
  23. >64k.  I allocate arrays of around 100k or more.
  24. >   My burning question is, can I use a structure rather then the above
  25. >method for accessing large arrays?  I'm using Turbo C++ version 3.0, and
  26. >I'm in the large memory model.  I'm also setting the pointer to memory
  27. >as a far huge pointer.
  28.  
  29. >[Tim]
  30.  
  31. A couple of things.  
  32. First your 64k problem.  malloc has a 64k size limit.  To allocate a
  33. huge array you must use halloc.  The compilers I've used (i'm not
  34. certain that this is true in all compilers) require huge arrays
  35. allocated with halloc larger than 128k to have an elelment size that
  36. is a power of 2.  Therefore the statement-
  37.  
  38. ptr = halloc(1000, 3);  // 1000 elements at 3 bytes- total 3000 bytes
  39.  
  40. is illegal.  But-
  41.  
  42. ptr = halloc(750, 4);   // 750 elements at 4 bytes- total 3000 bytes
  43.  
  44. is legal.  That should take care of your size limit problem.
  45.  
  46. Second is the multiplication to access the data.  Why not use a 2
  47. dimensional array?  It's a little more overhead setting it up but
  48. worth the effort for speeds sake.  My solution looks something like
  49. this- (just a quick hack with little error checking)-
  50.  
  51. // Set up dynamic 2 dimensional array
  52. void make_array(int x_dim, int y_dim)
  53. {
  54. int **ptr;        // Assuming a dual array of ints
  55.  
  56.     // Allocate x dimension.
  57.     ptr = halloc(x_dim, sizeof(int));    // Assuming int is 2 or 4
  58. bytes
  59.     
  60.     // Allocate y dimension
  61.     for(indx = 0; indx < y_dim; indx++)
  62.         ptr[indx] = halloc(y_dim, sizeof(int));
  63.  
  64. // Have to free the array
  65. void free_array(int x_dim, int y_dim)
  66. {
  67. int indx;
  68.  
  69.     // Free y dimension
  70.     for(indx = 0; indx < y_dim; indx++)
  71.         hfree(ptr[indx]);
  72.  
  73.     // Free x dimension
  74.     hfree(ptr);
  75. }
  76.  
  77. Now any point in your array can be accessed very easily by-
  78.  
  79. data = ptr[x][y];
  80.  
  81. instead of the single dimensional model-
  82.  
  83. data = ptr[x*x_dim+y];
  84.  
  85. Hope this helps.
  86. ~Gerry
  87.  
  88.